package org.zjx.swing.controller;

import org.zjx.swing.model.SceneModel;
import org.zjx.swing.view.SceneView;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SceneController {
    private SceneModel model;
    private SceneView view;

    public SceneController(SceneModel model, SceneView view) {
        this.model = model;
        this.view = view;
        setupListeners();
    }

    private void setupListeners() {
        view.addOpenButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) {
                    try {
                        model.loadXmlFile(chooser.getSelectedFile().getAbsolutePath());
                        refreshAllTables();
                        view.showStatus("成功加载文件");
                    } catch (Exception ex) {
                        view.showError("加载失败: " + ex.getMessage());
                    }
                }
            }
        });

        view.addSaveButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (model.getCurrentFilePath() == null) {
                        JFileChooser chooser = new JFileChooser();
                        if (chooser.showSaveDialog(view) == JFileChooser.APPROVE_OPTION) {
                            model.setCurrentFilePath(chooser.getSelectedFile().getAbsolutePath());
                        } else {
                            return;
                        }
                    }
                    model.validateScene(model.getLoadedScene());
                    model.saveXmlFile(model.getCurrentFilePath());
                    view.showStatus("文件保存成功");
                } catch (Exception ex) {
                    view.showError("保存失败: " + ex.getMessage());
                }
            }
        });

        view.addSaveAsButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileFilter(new FileNameExtensionFilter("XML Files", "xml"));
                if (chooser.showSaveDialog(view) == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = chooser.getSelectedFile();
                    String filePath = selectedFile.getAbsolutePath();
                    if (!filePath.toLowerCase().endsWith(".xml")) {
                        selectedFile = new File(filePath + ".xml");
                    }
                    if (selectedFile.exists()) {
                        int overwrite = JOptionPane.showConfirmDialog(
                                view,
                                "文件已存在，是否覆盖？",
                                "确认覆盖",
                                JOptionPane.YES_NO_OPTION
                        );
                        if (overwrite != JOptionPane.YES_OPTION) return;
                    }
                    try {
                        model.validateScene(model.getLoadedScene());
                        model.setCurrentFilePath(selectedFile.getAbsolutePath());
                        model.saveXmlFile(model.getCurrentFilePath());
                        view.showStatus("文件保存成功");
                    } catch (Exception ex) {
                        view.showError("保存失败: " + ex.getMessage());
                    }
                }
            }
        });



        view.addHelpButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                view.showHelp();
            }
        });
    }

    private void refreshAllTables() {
        view.refreshSceneTable(model.getLoadedScene());
        view.refreshNpcTable(model.getLoadedScene().getNpcs());
        view.refreshRoomTable(model.getRoomList());
        view.refreshWayPointTable(model.getWayPointList());
    }
}